<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: PortalPage.php 467 2011-01-25 17:24:30Z steen.rabol $
 * @package Pradoportal.Common
 *
 */

Prado::using('Application.Common.PortalBasePage');
Prado::using('Application.Common.Data.PortalPagecontentRecord');
/**
 *
 * @package Pradoportal.Common
 */
class PortalPage extends PortalBasePage
{
	private $_obportlets	= array();	// 'Out of bounce' portlets - Portlets that don't belongs to a layout area in the current layout

	public function onPreInit($param)
	{
		$layoutname	= 'PortalLayout';
		parent::onPreInit($param);

		if(!$this->PageRow)
		{
			$this->reportError(1,Prado::localize("The requested page is not valid: {0} ", $this->Service->RequestedPagePath));
		}

		if($this->PageRow->layoutname !== null && $this->PageRow->layoutname != '')
		{
			$layoutname	= $this->PageRow->layoutname;
		}

		$layoutfile = $this->Application->PortalBasePath . "/protected/Layouts/Portal/" . $this->Application->Parameters['ThemeName'] . "/" . $layoutname  . ".php";
		if(file_exists($layoutfile))
		{
			$this->MasterClass	= 'Application.Layouts.Portal.' .  $this->Application->Parameters['ThemeName'] . "." . $layoutname;
		}
		else
		{
			Prado::log("Theme: " . $this->Application->Parameters['ThemeName'] . " does not contain template: " . $layoutname . " used at page " . $this->PageRow->name . ": $layoutfile",TLogger::ALERT);
			$this->MasterClass	= 'Application.Layouts.Portal.' .  $this->Application->Parameters['ThemeName'] . ".PortalLayout";
		}

		$this->Theme		= "Portal/" . $this->Application->Parameters['ThemeName'];
		// Unlock page.
		if(TPropertyValue::ensureBoolean($this->Request['unlock'])/* || $this->EditMode === 0*/)
		{
			if($this->PageRow->locked && ($this->PageRow->lockedby == $this->User->Id))
			{
				$this->PageRow->locked		= 0;
				$this->PageRow->lockedby	= 0;
				$this->PageRow->lockedtime	= 0;
				$this->PageRow->save();
			}
		}
	}

	public function onLoad($parm)
	{
		if($this->EditMode === 0 && TPropertyValue::ensureInteger($this->PageRow->active) === 0)
		{

			PortalUtil::Log("Not activ page requested: " . $this->PageRow->displayname . " " . $this->PageRow->fullpath);
			$this->reportError(1,Prado::localize("Page is not active"));
		}

		if($this->EditMode == 1 && $this->CanEdit)
		{
			if($this->PageRow->locked && ($this->PageRow->lockedby != $this->User->Id))
			{
				$lockedrow = $this->DataAccess->createCommand("select name,full_name from tblusers where id=" . $this->PageRow->lockedby)->query()->read();
				$this->reportError(1,"You can not edit this page as it is locked by: " . $lockedrow['name'] . ", " . $lockedrow['full_name'] . " at " . date("d-m-Y H:i:s", $this->PageRow->lockedtime));
			}
			else
			{
				if($this->PageRow->locked == 0)
				{
					$this->PageRow->locked		= 1;
					$this->PageRow->lockedby	= $this->User->Id;
					$this->PageRow->lockedtime	= time();
					$this->PageRow->save();
				}
			}
		}

		$criteria = new TActiveRecordCriteria;
		$criteria->Condition = 'pagename = :pagename and active=:active and culture=:culture';
		$criteria->Parameters[':pagename'] = $this->Service->RequestedPagePath;
		$criteria->Parameters[':active'] = 1;
		$criteria->Parameters[':culture'] = $this->Application->Culture;

		$criteria->OrdersBy['displayorder'] = 'asc';
		
		$content = PortalPagecontentRecord::finder()->findAll($criteria);
		
		if($content !== false && $content !== null && count($content) > 0)
		{
			foreach($content as $c)
			{
				if((TPropertyValue::ensureInteger($c->showafterlogin) == 0 && !$this->User->IsGuest && $this->EditMode == 0) || ($c->needlogin == 1 && $this->user->IsGuest && $this->EditMode == 0))
				{
					continue;
				}

				$ctrl = $this->Master->findControl($c->controlname);
				if($ctrl !== null )
				{
					try
					{
						$portletpath = "Application.Portlets." . $c->portlet . ".";
						if($this->EditMode == 1 && $this->CanEdit)
						{
							 $portletpath .= "Edit.";
						}

						$portletpath .= $c->portlet;
						Prado::using($portletpath);
						$clsname = $c->portlet;
						$cls = new $clsname($c->toArray());
						$ctrl->Controls[] = $cls;

						if($this->EditMode == 0)
						{
							if($ctrl->ControlSpacing != '')
							{
								$spacer = new TPanel();
								$spacer->Style = "height:" . $ctrl->ControlSpacing;
								$ctrl->Controls[] = $spacer;
							}
						}
					}
					catch(Exception $e)
					{
						// We don't do anything...
						Prado::log("PortletPage: Portlet : '" . $c->portlet . "' not installed or not found $portletpath, Exception: " . $e->getMessage(),TLogger::ALERT);
					}
				}
				else
				{
					Prado::log("could not find: " . $c->controlname . " on page : " . $this->Service->RequestedPagePath,TLogger::ALERT);
					$this->ObPortlets = $c;
				}
			}
		}

		parent::onLoad($parm);

		$this->Title = $this->Application->Parameters['SiteTitle'] . " - " . $this->PageRow->displayname;

		$obctrls = $this->ObPortlets;
		if($this->EditMode == 1 && $this->CanEdit && count($obctrls) > 0)
		{
			// Add a control to contain the 'Out of bounce' contents
			$ctrl = $this->Master->findControl('Main');
			if($ctrl !== null)
			{
				$layctrl = new PPLayoutControl();
				$layctrl->Id	= "Out_of_bounce_controls";

				foreach($obctrls as $obctrl)
				{
					try
					{
						$portletpath = "Application.Portlets." . $obctrl->portlet . ".Edit." . $obctrl->portlet;
						Prado::using($portletpath);
						$clsname = $obctrl->portlet;
						$cls = new $clsname($obctrl->toArray());
						$layctrl->Controls[] = $cls;
					}
					catch(Exception $e)
					{
						Prado::trace($e->getMessage(), 'Info');
					}
				}
				
				$ctrl->Controls[] = $layctrl;
			}
		}
	}

	public function gotoDefaultPage()
	{
		$this->gotoPage($this->Service->DefaultPage,array("PageMode" => $this->EditMode));
	}

	/*
	 * Class properties
	 */

	public function getObPortlets()
	{
		return $this->_obportlets;
	}

	public function setObPortlets($value)
	{
		$this->_obportlets[] = $value;
	}
}
?>